package com.slf.flow.oss.service;

import com.slf.flow.oss.model.Oss;
import com.slf.flow.oss.utils.MinioOSSUtil;
import org.apache.commons.lang3.StringUtils;
import com.slf.flow.oss.mapper.OssMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@Service
public class OssService extends ServiceImpl<OssMapper, Oss> {

	@Resource
	private HttpServletResponse response;

	public Oss getOss(String ossId) {
		Oss oss = null;
		if(StringUtils.isNotBlank(ossId)) {
			oss = getBaseMapper().selectById(ossId);
		}else {
			oss = getBaseMapper().selectOne(new QueryWrapper<Oss>().orderByDesc("create_date"));
		}
		return oss;
	}
	public void downloadFile(String ossId, String path){
		Oss oss = getBaseMapper().selectById(ossId);
		MinioOSSUtil.downloadFile(oss,path,response);
	}

	public void uploadByUrl(String ossId, String url) {
		Oss oss = getBaseMapper().selectById(ossId);
		MinioOSSUtil.uploadByUrl(oss,url);
	}
}
